<?php
namespace MyClass;
class Gift extends \C\Action {
    //订单列表
    public function Lists(){
		if(!session('Openid')) {
			$this->errors('请使用微信扫码');
		}
		$GiftList=$this->m('gift_type')->select();
		$w['Openid'] = session('Openid');
		$OrderList=$this->m('gifts')-> join ('gift_type on gifts.GiftId= gift_type.Id')->where($w)->field('gifts.*,gift_type.Name')->select();
		$this->s('OrderList', $OrderList);
		$this->s('GiftList', $GiftList);
		$this->v();
	}
	//检测验证码
	public function CheckGiftNums() {
		if(!$_REQUEST['GiftNums']){
			$info = array('status'	=> false,'msg'=> '兑换码不能为空',);
			echo json_encode($info);exit;
		}
		$w['CodeMa'] = trim($_REQUEST['GiftNums']);
		$w['Ifsy'] = 1;
		$if=$this->m('gift_code')->where($w)->find();
		if($if) {
			$info = array('status'	=> true,'msg'=> '该兑换码可用',);
		}else {
			$info = array('status'	=> false,'msg'=> '该兑换码不可用',);
		}
		echo json_encode($info);exit;
	}
	//添加订单
	public function OrderAdd() {
		if(!$_REQUEST['GiftNums']||!$_REQUEST['Buyer']||!$_REQUEST['Phone']||!$_REQUEST['Address']){
			$this->error('请填写完整信息');
		}
		$ww['CodeMa'] = trim($_REQUEST['GiftNums']);
		$ww['Ifsy'] = 1;
		$res1=$this->m('gift_code')->where($ww)->find();
		if(!$res1){
			$this->error('兑换码不可用');
		}
		$add['Openid'] =session('Openid');
		$add['OrderNums'] = 'JSWR'.time();
		$add['GiftNums'] = trim($_REQUEST['GiftNums']);
		$add['Buyer'] = $_REQUEST['Buyer'];
		$add['Phone'] = $_REQUEST['Phone'];
		$add['Address'] = $_REQUEST['Address'];
		$add['GiftId'] = $res1['Gid'];
		$add['OrderTIme'] =  date("Y-m-d H:i",time());
		$add['Remark'] =  $_REQUEST['Remark'];
		$result=$this->m('gifts')->add($add);
		if($result) {
			$save['Ifsy'] = 2;
			$w['CodeMa'] = trim($_REQUEST['GiftNums']);
			$this->m('gift_code')->where($w)->save($save);
			$this->successs('兑换成功',T('Gift/Lists'));
		}else {
			$this->errors('兑换失败',T('Gift/Lists'));
		}
	}
	//查看订单
	public function OrderSee() {
		if(!$_REQUEST['Id']){
			$this->errors('参数错误',T('Gift/Lists'));
		}
		$w['o.Id'] = $_REQUEST['Id'];
		$oinfo = $this->m('gifts o')-> join ('gift_type on o.GiftId= gift_type.Id')->where($w)->field('o.*,gift_type.Name')->find();
		$this->s('oinfo', $oinfo);
		$this->v();
	}


	//报错页面
	public function errors($err,$url=''){
		if(!$url) {
			$url = $_SERVER['HTTP_REFERER'];
		}
		$this->s("err",$err);
		$this->s('url',$url);
		$this->v("Gift/errors");
		exit;
	}
	protected function successs($msg,$url=''){
		if(!$url) {
			$url = $_SERVER['HTTP_REFERER'];
		}
		$this->s('msg',$msg);
		$this->s('url',$url);
		$this->v('Gift/successs');
	}
}